package gqz.mvvm.viewmodel;

import gqz.mvvm.model.Account;
import gqz.mvvm.view.MainIF;

/**
 * Created by Administrator on 2017/11/29.
 */

public class MainVm {

    private final String TAG = "MainVm---";

    private MainIF mainIF;
    private Account account;

    private String code = "";

    public MainVm(MainIF mainIF) {
        account = new Account();
        this.mainIF = mainIF;
        mainIF.bindAccount(account);
        createCheckCode();
    }

    /**
     * 生成验证码
     */
    public void createCheckCode() {
        String code = "";
        for (int i = 0; i < 4; i++) {
            code += (int) (Math.random() * 10) + "";
        }
        this.code = code;
        mainIF.refreshCheckCode(code);
    }

    /**
     * 校对验证码
     */
    public void checkCode(String code) {
        if (!this.code.equals(code)) {
            mainIF.refreshCheckCodeIcon(false);
            return;
        }
        mainIF.refreshCheckCodeIcon(true);
    }

    /**
     * 登录验证
     */
    public void login(String name, String pwd, boolean checkState) {
        if (checkState)
            if (name.equals("gowcage") && pwd.equals("1234")) {
                mainIF.loginSuccess();
                return;
            }
        mainIF.loginFailed();
    }

}
